//
//  StatusCell.swift
//  WeiBo09
//
//  Created by 裘明 on 15/9/6.
//  Copyright © 2015年 qm. All rights reserved.
//

import UIKit
/// 控件间距
let statusCellMargin: CGFloat = 12
/// 头像的大小
let statusCellIconWidth: CGFloat = 35
/// 默认的图片大小
let statusPictureItemWidth: CGFloat = 90
/// 默认的图片间距
let statusPictureItemMargin: CGFloat = 8
/// 每行最大图片的数量
let statusCellPictureMaxCount: CGFloat = 3
/// 配图视图的最大尺寸
let statusCellPictureMaxWidth = statusCellPictureMaxCount * statusPictureItemWidth + (statusCellPictureMaxCount - 1) * statusPictureItemMargin

/// 微博cell
class StatusCell: UITableViewCell {
    
    /// 微博模型数据视图
    var statusViewModel: StatusViewModel? {
        didSet {
            topView.statusViewModel = statusViewModel
            contentLabel.text = statusViewModel?.status.text

            pictureView.statusViewModel = statusViewModel
            
            pictureViewWidthCons?.constant = pictureView.bounds.width
            pictureViewHightCons?.constant = pictureView.bounds.height
            pictureViewTopCons?.constant = statusViewModel?.thumbnailUrl?.count == 0 ? 0 : statusCellMargin
            // 调试代码
            // pictureViewHightCons?.constant = statusPictureItemWidth * CGFloat(random() % 4)
        }
    }
    /// 配图宽度约束
    var pictureViewWidthCons: NSLayoutConstraint?
    /// 配图高度约束
    var pictureViewHightCons: NSLayoutConstraint?
    /// 配图顶部约束
    var pictureViewTopCons: NSLayoutConstraint?

    
    func rowHeight(viewModel: StatusViewModel) -> CGFloat {
        // 1.设置视图模型
        statusViewModel = viewModel
        // 2.更新约束
        layoutIfNeeded()
        // 3.返回视图的最大高度
        return CGRectGetMaxY(bottomView.frame)
        
    }
    
    //  MARK: - 搭建界面
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    func setupUI() {
         /// 顶部分割视图
        let topSepView = UIView()
        topSepView.backgroundColor = UIColor(white: 0.85, alpha: 1)
        
        // 1.添加控件
        contentView.addSubview(topSepView)
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        
        // 2.设置约束
        let width = UIScreen.mainScreen().bounds.width
        // 1> 顶部分割视图
        topSepView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: width, height: 10))
        
        // 2> 顶部视图
        topView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topSepView, size: CGSize(width: width, height: statusCellMargin + statusCellIconWidth), offset: CGPoint(x: 0, y: 0))
        
        // 3> 文本标签
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: statusCellMargin, y: statusCellMargin))
                
        // 5.底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: width, height: 44), offset: CGPoint(x: -statusCellMargin , y: statusCellMargin))
        
        // 指定底部视图相对底边的约束
//        bottomView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: contentView, size: nil)
        
    }
    
    
    //  MARK: - 懒加载控件
    
    /// 1.顶部视图
    private lazy var topView: StatusCellTopView = StatusCellTopView()
    /// 2.文本标签
    lazy var contentLabel = UILabel(title: nil, color: UIColor.darkGrayColor(), fontSize: 15, layoutWidth:  UIScreen.mainScreen().bounds.width - 2 * statusCellMargin)
    /// 3.配图视图
    lazy var pictureView: StatusPictureView = StatusPictureView()
    /// 4.底部视图
    lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    
}
